// -----------------------------------------------------------------------
//  <copyright file="RoleController.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024/12/10 11:01:41</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Identity.Application.Identity.Roles.Commands;
using DaprPlus.Identity.Application.Identity.Roles.Queries;


namespace DaprPlus.Identity.Api.Controllers
{
    [ApiResultWrapperFilter]
    public class RoleController(IMediator mediator) : ApiControllerBase
    {
        [HttpGet("id/{id}")]
        [Produces<RoleOutDto>]
        public async Task<ActionResult> Get(long id)
        {
            var query = new GetRoleByIdQuery(id);
            var result = await mediator.Send(query);
            if (result == null)
            {
                return NotFound();
            }
            return Ok(result);
        }

        [HttpGet("name/{name}")]
        [Produces<RoleOutDto>]
        public async Task<ActionResult> Get(string name)
        {
            var query = new GetRoleByNameQuery(name);
            var result = await mediator.Send(query);
            if (result == null)
            {
                return NotFound();
            }
            return Ok(result);
        }

        [HttpGet("node")]
        [Produces<RoleOutDto>]
        public async Task<ActionResult> GetNode(long? userId = null)
        {
            var query = new GetRoleNodeQuery { UserId = userId };
            var result = await mediator.Send(query);
            return Ok(result);
        }

        [HttpPost("read")]
        [Produces<PageResult<RoleOutDto>>]
        public async Task<ActionResult> Read([FromBody] GetRolePageQuery query)
        {
            var page = await mediator.Send(query);
            return Ok(page);
        }

        [HttpPost]
        [Produces<ApiResult>]
        public async Task<ActionResult> Create([FromBody] CreateRoleCommand command)
        {
            var result = await mediator.Send(command);
            return Ok(result);
        }

        [HttpPut]
        [Produces<ApiResult>]
        public async Task<ActionResult> Update([FromBody] UpdateRoleCommand command)
        {
            var result = await mediator.Send(command);
            return Ok(result);
        }

        [HttpDelete()]
        [Produces<ApiResult>]
        public async Task<ActionResult> Delete(long[] ids)
        {
            var command = new DeleteRoleCommand(ids);
            var result = await mediator.Send(command);
            return Ok(result);
        }
    }
}
